package cn.waka.sys.admin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import cn.waka.sys.admin.object.Page;

@Component
public class FaceDao extends BaseDao{
	public List<Map<String,Object>> findList(Page p,Map<String,String> param){
		int page=p.getPage();
		int start=(page-1)*p.getSize();
		
		String sql="select id,url,c_time,description from b_face_package";
		Set<String> pks= param.keySet();
		Object[] dbparams=new Object[2+param.size()];
		int i=0;
		if(param.size()>0){
			for(String pa : pks){
				sql+=" and "+pa+" like ?";
				dbparams[i]=param.get(pa);
				i++;
			}
			
			dbparams[param.size()]=start;
			dbparams[param.size()+1]=p.getSize();
		}else{
			dbparams[0]=start;
			dbparams[1]=p.getSize();
		}
		sql+=" order by id desc limit ?,?";
		List<Map<String,Object>> list=this.query(sql, new RowMapper<Map<String,Object>>(){
			@Override
			public Map<String,Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<String,Object>();
				row.put("id", rs.getInt(1));
				row.put("url", rs.getString(2));
				row.put("c_time", rs.getString(3));
				row.put("description", rs.getString(4));
				return row;
			}
			
		},dbparams);
		return list;
	}
	
	public int findCount(Page p,Map<String,String> param){
		String sql="select count(1) from b_face_package";
		Set<String> pks= param.keySet();
		Object[] dbparams=new Object[param.size()];
		int i=0;
		if(param.size()>0){
			for(String pa : pks){
				sql+=" and "+pa+" like ?";
				dbparams[i]=param.get(pa);
				i++;
			}
		}
		int c=0;
		if(param.size()>0){
			c=this.queryForObject(sql,Integer.class,dbparams);
		}else{
			c=this.queryForObject(sql,Integer.class);
		}
		return c;
	}
	
	
	public Map<String,Object> findById(int id){
		String sql="select id,url,description from b_face_package where id=?";
		List<Map<String,Object>> list=this.query(sql, new RowMapper<Map<String,Object>>(){
			@Override
			public Map<String,Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<String,Object>();
				row.put("id", rs.getInt(1));
				row.put("url", rs.getString(2));
				row.put("description", rs.getString(3));
				return row;
			}
			
		},id);
		return list.size()>0?list.get(0):new HashMap<String,Object>();
	}
	
	public int insert(String url, String description) {
		KeyHolder holder = new GeneratedKeyHolder();
		this.update(new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				String sql = "insert into b_face_package(url,`description`)values(?,?)";
				PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, url);
				ps.setString(2, description);
				return ps;
			}
		}, holder);
		int id = holder.getKey().intValue();
		return id;
	}
	
	public int update(int id, String url, String description) {
		int rs = this.update("update b_face_package set url=?,`description`=? where id=?",
				url,
				description,
				id);
		return rs;
	}

	public String findNewUrl(){
		String sql="select url from b_face_package order by id desc limit 1";
		String url = this.queryForObject(sql,String.class);
		return url!=null?url:"";
	}
	
	public int del(int id) {
		int r = this.update("delete from b_face_package where id=?",id);
		return r;
	}
}
